package com.tedu.springboot2302.controller;

import com.tedu.springboot2302.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {
    private static File userDir;
    static{
        userDir=new File("./users");
        if(!userDir.exists()){
            userDir.mkdirs();
        }
    }

   @RequestMapping("/regUser")
    public void reg(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理用户注册");
        //获取表单提交的信息
        //getParameter()中的参数要和表单中输入框名字一致
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String nickname=request.getParameter("nickname");
        String ageStr=request.getParameter("age");
        System.out.println(username+"、"+password+"、"+nickname+"、"+ageStr);

        if(username==null||username.isEmpty()||
                password==null||password.isEmpty()||
                nickname==null||nickname.isEmpty()||
                ageStr==null||ageStr.isEmpty()||!ageStr.matches("[0-9]+")){

            try {
                response.sendRedirect("/reg_info_error.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        int age=Integer.parseInt(ageStr);
        User user=new User(username,password,nickname,age);
        File file=new File(userDir,username+".obj");
        if(file.exists()){
            try {
                response.sendRedirect("/have_user.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        try (
            FileOutputStream fos=new FileOutputStream(file);
            ObjectOutputStream oos=new ObjectOutputStream(fos)
        ){
            oos.writeObject(user);

            response.sendRedirect("/reg_success.html");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    @RequestMapping("/loginUser")
    public void login(HttpServletRequest request,HttpServletResponse response){

        String username=request.getParameter("username");
        String password=request.getParameter("password");

        if(username==null||username.isEmpty()||password==null||password.isEmpty()) {
            try {
                response.sendRedirect("/login_info_error.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        File file=new File(userDir,username+".obj");
        if(!file.exists()){
            try {
                response.sendRedirect("/login_fail.html");

            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }


        try (
            FileInputStream fis=new FileInputStream(file);
            ObjectInputStream ois=new ObjectInputStream(fis);)
        {
            User user = (User) (ois.readObject());

            if(user.getPassword().equals(password)){
                try {
                    response.sendRedirect("/login_success.html");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return;
            }
            response.sendRedirect("/login_fail.html");

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }








    }


    @RequestMapping("/userList")
    public void userList(HttpServletRequest request,HttpServletResponse response){

        System.out.println("开始处理显示用户列表");
        List<User> userList=new ArrayList<>();

        File[] subs=userDir.listFiles(f->f.getName().endsWith(".obj"));
        for(File sub:subs){
            try (
                    FileInputStream fis=new FileInputStream(sub);
                    ObjectInputStream ois=new ObjectInputStream(fis)
                    ){

                User user=(User)ois.readObject();
                userList.add(user);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }


        response.setContentType("text/html;charset=utf-8");


        try {
            PrintWriter pw=response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>用户列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>用户列表</h1>");

            pw.println("<table border=\"1\">");
            pw.println("<tr>");
            pw.println("<td>用户名</td>");
            pw.println("<td>密码</td>");
            pw.println("<td>昵称</td>");
            pw.println("<td>年龄</td>");
            pw.println(" </tr>");

            for(User user : userList) {
                pw.println("<tr>");
                pw.println("<td>"+user.getUsername()+"</td>");
                pw.println("<td>"+user.getPassword()+"</td>");
                pw.println("<td>"+user.getNickname()+"</td>");
                pw.println("<td>"+user.getAge()+"</td>");
                pw.println("<tr>");
            }

            pw.println("</table>");

            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
